package com.ddxcloud.common.utils;

import com.ddxcloud.common.constants.enums.ResultCodeEnum;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 8004487252556526569L;

    private Integer status;
    private String msg;
    private T data;

    /**
     * 统一返回
     */

    // 统一成功返回
    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setStatus(ResultCodeEnum.SUCCESS.getCode());
        result.setMsg(ResultCodeEnum.SUCCESS.getMsg());
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success(String message, T data) {
        Result<T> result = new Result<>();
        result.setStatus(ResultCodeEnum.SUCCESS.getCode());
        result.setMsg(message);
        result.setData(data);
        return result;
    }

    // 统一错误返回
    public static <T> Result<T> fail() {
        Result<T> result = new Result<>();
        result.setStatus(ResultCodeEnum.ERROR.getCode());
        result.setMsg(ResultCodeEnum.ERROR.getMsg());
        return result;
    }

    public static <T> Result<T> fail(String message) {
        Result<T> result = new Result<>();
        result.setStatus(ResultCodeEnum.ERROR.getCode());
        result.setMsg(message);
        return result;
    }

    public static <T> Result<T> fail(String message, ResultCodeEnum codeEnum) {
        Result<T> result = new Result<>();
        result.setStatus(codeEnum.getCode());
        result.setMsg(message);
        return result;
    }
}
